package com.yonyou.brigade.third_parties.yihao.controller;

import com.alibaba.fastjson.JSONObject;
import com.yonyou.brigade.basic_init.model.BriChannel;
import com.yonyou.brigade.basic_init.service.IBriChannelService;
import com.yonyou.brigade.basic_init.service.IUserCenterService;
import com.yonyou.brigade.third_parties.yihao.service.ICLYHUserService;
import com.yonyou.brigade.third_parties.yihao.service.IYihaoDeptService;
import com.yonyou.ybzboot.starter.common.exception.BusinessException;
import com.yonyou.ybzboot.uc.dto.UcUserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/10/18.
 */
@Controller
@RequestMapping("/yihao")
public class SSOTestController {

    @Autowired
    IBriChannelService briChannelService;

    @Autowired
    IYihaoDeptService yihaoDeptService;

    @Autowired
    ICLYHUserService iclyhUserService;

    @Autowired
    @Qualifier("userCenterServiceImpl")
    private IUserCenterService userCenterService;

    @RequestMapping(value = "/getSsoUrl",method = RequestMethod.POST)
    public @ResponseBody
    Map<String, String> testUcUser(@RequestBody JSONObject body){
        Map<String,String> result = new HashMap<>();
        //iclyhPublicService.ssoLogin();
        result.put("result","成功");
        return result;
    }


    /**
     * 同步根级部门
     * @param param
     * @return
     */
    @RequestMapping(value = "/sysRootDept", method = RequestMethod.POST)
    public @ResponseBody Map<String, String> sysRootDept(@RequestBody JSONObject param) {
        String tenant = param.getString("tenant");
        String org = param.getString("org");
        String key = param.getString("key");
        if (!"yonyou@brigade".equals(key)){
            throw new BusinessException("非法操作");
        }
        BriChannel briChannel = briChannelService.getEnableInfoByTenantOrg(tenant, org, "clyh");
        if (briChannel == null) {
            throw new BusinessException("该组织未开通商旅，请联系组织管理员");
        }
        if (BriChannel.SYNCORGDEPT == briChannel.getSyncOrgDept()) {
            String config = briChannel.getConfig();
            JSONObject briConfig = JSONObject.parseObject(config);
            yihaoDeptService.sysRootDept(tenant,briConfig);
        }
        Map<String,String> rs = new HashMap<>();
        rs.put("msg", "操作完毕");
        return rs;
    }


    @RequestMapping(value = "/sysUserByOrg", method = RequestMethod.POST)
    public @ResponseBody Map<String, String> sysUserByOrg(@RequestBody JSONObject param) {
        String tenant = param.getString("tenant");
        String org = param.getString("org");
        String key = param.getString("key");
        String channel = param.getString("channel");
        if (!"yonyou@brigade".equals(key)){
            throw new BusinessException("非法操作");
        }
        BriChannel briChannel = briChannelService.getEnableInfoByTenantOrg(tenant, org, channel);
        if (briChannel == null) {
            throw new BusinessException("该组织未开通商旅，请联系组织管理员");
        }

        List<UcUserDto> ucUserDtoList = userCenterService.getUcUserDtoByOrg(tenant, org);
        if (BriChannel.SYNCUSER == briChannel.getSyncUser()) {
            iclyhUserService.sysClyhUsers(tenant, org, ucUserDtoList, briChannel);
        }
        Map<String,String> rs = new HashMap<>();
        rs.put("msg", "操作完毕");
        return rs;
    }
}